package main

import (
	"fmt"
	"time"
)

func main() {
	// 默认错误处理方法
	// error.Error()

	// 正常情况
	if result, errorMsg := Divive(100, 10); errorMsg == "" {
		fmt.Println("100 / 10 = ", result)
	}

	// 除数为0返回错误信息
	if _, errorMsg := Divive(100, 0); errorMsg != "" {
		fmt.Println("errorMsg is:", errorMsg)
	}

	go sayHi("张三")
	sayHi("李四")
}

/**
 * 错误结构体
 */
type DivideError struct {
	dividee int
	divider int
}

/**
 * 实现error接口
 */
func (divideError *DivideError) Error() string {
	strFormat := `
		cannot proceed, divider is zero
		dividee: %d
		divider: 0
	`
	return fmt.Sprintf(strFormat, divideError.dividee)
}

/**
 * 创建除法，函数名大写则表示public 小写则相当于protected
 * 定义int类型除法运算函数
 */
func Divive(dividee, divider int) (result int, errorMsg string) {
	if divider == 0 {
		dataError := DivideError{
			dividee: dividee,
			divider: divider,
		}
		errorMsg = dataError.Error()
		return
	} else {
		return dividee / divider, ""
	}
}

func sayHi(name string) {
	for i := 0; i < 5; i++ {
		time.Sleep(100 * time.Microsecond)
		println("hello", name)
	}

}
